﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Sunny.UI;
using DayuCode.Designer.Entity;
using DayuCode.Designer.Service;

namespace DayuCode.Designer.Forms
{
    public partial class FormRefColumn : UIForm
    {
        long projectId;
        HDataColumn srcColumnInfo;
        public FormRefColumn(long projectId, HDataColumn srcColumnInfo)
        {
            this.projectId = projectId;
            this.srcColumnInfo = srcColumnInfo;

            InitializeComponent();
            txtTableName.Text = srcColumnInfo.RefTableName;

            LoadAndBindColumns();

            cmbIdColumn.Text = srcColumnInfo.RefIdColumn;
            cmbNameColumn.Text = srcColumnInfo.RefNameColumn;
            txtNameColumnAliasName.Text = srcColumnInfo.RefNameColumnAliasName;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string refTableName = txtTableName.Text.Trim();
            string refIdColumn = cmbIdColumn.Text.Trim();
            string refNameColumn = cmbNameColumn.Text.Trim();
            string refNameColumnAliasName = txtNameColumnAliasName.Text.Trim();
            if (string.IsNullOrEmpty(refTableName))
            {
                MessageBox.Show("请填写参考表名称");
                return;
            }

            if (string.IsNullOrEmpty(refIdColumn))
            {
                MessageBox.Show("请选择Id字段名称");
                return;
            }
            if (string.IsNullOrEmpty(refNameColumn))
            {
                MessageBox.Show("请选择名称字段名称");
                return;
            }
            if (string.IsNullOrEmpty(refNameColumnAliasName))
            {
                MessageBox.Show("名称字段的别名不能为空");
                return;
            }
            
            srcColumnInfo.RefTableName = refTableName;
            srcColumnInfo.RefIdColumn = refIdColumn;
            srcColumnInfo.RefNameColumn = refNameColumn;
            srcColumnInfo.RefNameColumnAliasName = refNameColumnAliasName;
            this.Close();
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void txtTableName_Leave(object sender, EventArgs e)
        {
            LoadAndBindColumns();
        }

        string preTableName = "";
        private void LoadAndBindColumns()
        {
            string tableName = txtTableName.Text.Trim();
            if (tableName.Equals(preTableName)) return;
            if (tableName.Length > 0)
            {
                IList<HDataColumn> columns = null;
                try
                {                    
                    columns = DataTableHandler.getDataColumnsByTableName(projectId, tableName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
                IList<string> columnNames1 = new List<string>();
                IList<string> columnNames2 = new List<string>();
                foreach (HDataColumn col in columns)
                {
                    columnNames1.Add(col.ColumnName);
                    columnNames2.Add(col.ColumnName);
                }

                cmbIdColumn.DataSource = columnNames1;
                cmbNameColumn.DataSource = columnNames2;                
                preTableName = tableName;
            }
        }

        private void cmbNameColumn_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbNameColumn.SelectedItem == null) return;
            string tableName = txtTableName.Text.Trim();
            string tableName2 = tableName.Substring(tableName.LastIndexOf("_") + 1);
            string aliasName = tableName2 + "_" + cmbNameColumn.SelectedItem.ToString();

            txtNameColumnAliasName.Text = aliasName;
        }

        private void btnClearRefTable_Click(object sender, EventArgs e)
        {
            txtTableName.Clear();
            cmbIdColumn.SelectedIndex = -1;
            cmbNameColumn.SelectedIndex = -1;
            txtNameColumnAliasName.Clear();
            srcColumnInfo.RefTableName = "";
            srcColumnInfo.RefIdColumn = "";
            srcColumnInfo.RefNameColumn = "";
            srcColumnInfo.RefNameColumnAliasName = "";
            this.Close();
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
